package client;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.MatteBorder;



public class MapButtonHandler implements ActionListener {
	private final List<JButton> buttonList;
	private static final int DISABLED = 0;
	private static final int CURRENT = 1;
	private static final int AVAILABLE = 2;
	private final ClientController controller;
	private final ImageIcon borderIcon;

	//
	public MapButtonHandler(final ClientController controller) {
		this.controller = controller;
		this.buttonList = this.controller.getGUI().getMapButtons();
		setContentAreaNotFilled();
		addMouseListeners();
		this.borderIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("gui/border.jpg")));
	}


	public void setTileStatus(final List<Integer> stateList) {
		for (int i = 0; i < stateList.size(); i++) {
			final JButton currentButton = this.buttonList.get(i);
			if (stateList.get(i).equals(DISABLED)) {
				currentButton.setEnabled(false);
				currentButton.setBorderPainted(false);
				currentButton.removeActionListener(this);
			} else if (stateList.get(i).equals(CURRENT)) {
				currentButton.setEnabled(false);
				currentButton.setBorderPainted(true);
				currentButton.setBorder(javax.swing.BorderFactory
						.createLineBorder(this.controller
								.getPlayerColor(this.controller
										.getThisPlayerNumber()), 5));
				currentButton.removeActionListener(this);
			} else if (stateList.get(i).equals(AVAILABLE)) {
				// AVAILABLE
				currentButton.setEnabled(true);
				currentButton.setBorderPainted(true);
				currentButton.setBorder(new MatteBorder(this.borderIcon));
				// currentButton.setBorder(javax.swing.BorderFactory.createLineBorder(
				// new java.awt.Color(0, 255, 0), 5));
				currentButton.addActionListener(this);
			}
		}
	}


	public void hideAllButtons() {
		// Sets all buttons to disabled and invisible
		for (int i = 0; i < this.buttonList.size(); i++) {
			final JButton currentButton = this.buttonList.get(i);
			currentButton.setBorderPainted(false);
			currentButton.setEnabled(false);
		}
	}
	
	public void showAllPlayers(final List<Integer> positions) {
		for (int e = 0; e < positions.size(); e++) {
			// For each player position, draw a border of the player's color
			// but only if a border is not currently drawn (which happens if it
			// is an available position.)
			if (!this.buttonList.get(positions.get(e).intValue()).isBorderPainted()) {
				this.buttonList.get(positions.get(e).intValue()).setBorder(
						javax.swing.BorderFactory.createLineBorder(
								this.controller.getPlayerColor(e), 5));
				this.buttonList.get(positions.get(e).intValue()).setBorderPainted(true);
			}
					
		}
	}

	public void setContentAreaNotFilled() {
		for (int i = 0; i < this.buttonList.size(); i++) {
			this.buttonList.get(i).setContentAreaFilled(false);
		}
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		final JButton source = (JButton) e.getSource();
		for (int i = 0; i < this.buttonList.size(); i++) {
			if (this.buttonList.get(i) == source) {
				this.controller.tileChosen(i);
				this.controller
						.changeGameBoardInfoPanel("drawnAdventureCardsCard");
				
				
			}
		}
	}

	public void addMouseListeners() {
		for (int i = 0; i < this.buttonList.size(); i++) {
			final int e = i;
			// System.out.println(i + ", " + e +"\n");
			this.buttonList.get(i).addMouseListener(
					new java.awt.event.MouseAdapter() {
						@Override
						public void mouseEntered(
								final java.awt.event.MouseEvent evt) {
							MapButtonHandler.this.controller
									.changeBoardTileView(e);
						}

						@Override
						public void mouseExited(
								final java.awt.event.MouseEvent evt) {
							MapButtonHandler.this.controller
									.changeGameBoardInfoPanelToLast();
						}
					});
		}
	}

}
